﻿using System;
using System.Collections;
using System.Configuration;

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class admindata : System.Web.UI.Page
    {
        private int editid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterSubject"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            else
            {
                editid = 0;
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    SubjectInfo subjectInfo = new SubjectInfo();
                    subjectInfo = Subject.GetSubjectInfoById(editid);
                    Name.Text = subjectInfo.Name;
                    SubjectKeyword.Text = subjectInfo.Keyword;
                }
                RefurbishSubject();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Name.Text.Trim() != "" && SubjectKeyword.Text.Trim() != "")
            {
                SubjectInfo subjectInfo = new SubjectInfo();
                subjectInfo.Name = Name.Text;
                subjectInfo.Keyword = SubjectKeyword.Text;
                if (editid > 0)
                {
                    if (Subject.CheckSubjectExistByKeyword(SubjectKeyword.Text.Trim(), editid))
                    {
                        Utility.ShowBoxyAlert("关键字已存在！", this);
                    }
                    else
                    {
                        subjectInfo.Id = editid;
                        Subject.EditSubject(subjectInfo);
                        Response.Redirect("admindata.aspx");
                    }
                }
                else
                {
                    if (Subject.CheckSubjectExistByName(Name.Text.Trim(), SubjectKeyword.Text.Trim()))
                    {
                        Utility.ShowBoxyAlert("该课程名称或关键字已经存在！", this);
                    }
                    else
                    {
                        Subject.AddSubject(subjectInfo);
                        Response.Redirect("admindata.aspx");
                    }
                }
            }
            else
            {
                Utility.ShowBoxyAlert("课程名称和关键字不能为空！", this);
            }
        }

        protected void RefurbishSubject()
        {
            IList<SubjectInfo> SubjectInfos = Subject.GetSubjectInfos();
            StringBuilder sb = new StringBuilder();
            foreach (SubjectInfo info in SubjectInfos)
            {
                sb.Append("<div id=\"block\">");
                sb.Append("<div id=\"blocktitle\"><h3><a href=\"javascript:ShowIframe('adminKnowledge.aspx?subjectid=");
                sb.Append(info.Id.ToString());
                sb.Append("','管理知识点',600,500);\">");
                sb.Append(info.Name);
                sb.Append("</a></h3> <em>[<a href='admindata.aspx?editid=" + info.Id + "'>编辑</a> | <a href='javascript:ShowConfirm(\"PostSubject.aspx?delid=" + info.Id + "\",\"本操作不可逆，是否继续？\",150,60)'>删除</a> | <a href='javascript:ShowIframe(\"PostBook.aspx?SubjectID=" + info.Id + "\",\"新建教材版本\",600,50);'>新建教材版本</a> | <a href='javascript:ShowIframe(\"SubjectAbility.aspx?SubjectId=" + info.Id + "\",\"学科能力\",500,300);'>学科能力</a>]</em>");
                sb.Append("</div>");
                sb.Append("<div id=\"blockcontent\">");
                sb.Append("<ul id='hlist' class='clearfix2'>");
                sb.Append(RefurbishBook(info.Id));
                sb.Append("</ul>");
                sb.Append("</div>");
                sb.Append("</div>");
            }
            DataView.Text=sb.ToString();
        }

        protected string RefurbishBook(int SubjectId)
        {
            IList<BookInfo> BookInfos = Book.GetBookInfosBySubject(SubjectId, 0, 0);
            StringBuilder sb = new StringBuilder();
            foreach (BookInfo info in BookInfos)
            {
                sb.Append("<li>");
                sb.Append(info.Name);
                sb.Append("<span>[<a href='javascript:ShowConfirm(\"PostBook.aspx?delid=");
                sb.Append(info.Id);
                sb.Append("&SubjectID=");
                sb.Append(info.Subject);
                sb.Append("\",\"本操作不可逆，是否继续？\",150,60)'>删除</a> | <a href='javascript:ShowIframe(\"PostBook.aspx?editid=");
                sb.Append(info.Id);
                sb.Append("&subjectid=");
                sb.Append(info.Subject);
                sb.Append("\",\"编辑教材版本\",600,50);'>编辑</a>]</span>");
                sb.Append("</li>");
            }
            return sb.ToString();
        }
    }
}
